package com.etc.controller;


import com.etc.entity.Questionnaire;
import com.etc.entity.User;
import com.etc.mapper.QuestionManagerMapper;
import com.etc.service.QuestionnaireService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/Manager")
public class QuestionManager {
    @Autowired
    private QuestionnaireService questionnaireService;
    @Autowired
    private QuestionManagerMapper questionManagerMapper;
    @RequestMapping(value = "/queryAll")
    public String  query(@RequestParam(value = "pageNum" ,required = false)String pageNum, @RequestParam(value = "username",required = false)String username, Model model){
        System.out.println(username);
        //当前页数
        if(pageNum==null){
            pageNum="1";
        }

        //每页条数
        int pageSize=5;

        Integer PageNum = Integer.valueOf(pageNum);
        PageHelper.startPage(PageNum, pageSize);
        if(username==null){
            List<Questionnaire> questionnaires = questionnaireService.querybyAll();
            model.addAttribute("questionnaires",questionnaires);
        }else{
            List<Questionnaire> questionnaires = questionManagerMapper.querybyTest(username);
            model.addAttribute("questionnaires",questionnaires);
        }
        Questionnaire querycount = questionManagerMapper.querycount();
        int count =querycount.getCount();
        model.addAttribute("count",count);
        model.addAttribute("pageNum",pageNum);

        //总页数
        int totalNum=count%pageSize==0?count/pageSize:count/pageSize+1;
        model.addAttribute("totalNum",totalNum);
        return "QuestionManager";
    }

}
